prefix ?= /usr/local
OUTDIR=$(shell pwd)/build
CC=gcc
DEMO=net-tool
LIB=uos-net-kit
CFLAGS= -O3 -Wall
INC=-I./inc
LIBS=-lpthread -lncurses -fPIC -g
DEMO_SRC = $(wildcard test/*.c)
DEMO_OBJ = $(DEMO_SRC:.c=.o)
_DEMO_OBJ = $(addprefix ${OUTDIR}/demo/, $(notdir ${DEMO_OBJ}))
#SRC = $(filter-out ${DEMO_SRC}, $(wildcard ./*.c))
SRC = $(wildcard src/*.c)
SRC_OBJ=$(SRC:.c=.o)
_SRC_OBJ=$(addprefix ${OUTDIR}/, $(notdir $(SRC_OBJ)))

SUBDIRS=dpdk

SRCDIR=$(shell pwd)

LIBDIR=${OUTDIR}/lib
export OUTDIR SRCDIR LIBDIR

all: init ${LIB} ${DEMO} ${SUBDIRS}

ifeq ($(wildcard ${OUTDIR}),)
init:
	@mkdir -p ${OUTDIR}
	@mkdir -p ${OUTDIR}/demo
	@mkdir -p ${OUTDIR}/lib
else
init:
	@rm -rf ${OUTDIR}
	@mkdir -p ${OUTDIR}
	@mkdir -p ${OUTDIR}/demo
	@mkdir -p ${OUTDIR}/lib
endif

#${DEMO}:${SRC_OBJ}
#	$(CC) $(CFLAGS) -o $@ $^ $(LIBS)



${DEMO}: ${_DEMO_OBJ}
	${CC} -L${LIBDIR} -l${LIB} ${LIBS} -ldl $^ -o ${OUTDIR}/$@ 
	#${CC} -Wl,-Bstatic -L./ -luos-udp -Wl,-Bdynamic ${LIBS} $^ -o $@ 

${_DEMO_OBJ}: ${DEMO_OBJ}
	@echo ${DEMO_OBJ}
	@echo ${DEMO_SRC}

${DEMO_OBJ}:%.o:%.c
	@echo $@
	$(CC) $(CFLAGS) $(LIBS) -c $^ ${INC} -o ${OUTDIR}/demo/$(notdir $@) 

${LIB}: ${_SRC_OBJ} 
	$(CC) -shared $(CFLAGS) ${LIBS}  $^ ${INC} -o ${OUTDIR}/lib/lib${LIB}.so

#${LIB}: ${SRC_OBJ} 
#	rm -f $@
#	ar -r $@ ${SRC_OBJ}
#	rm -f ${SRC_OBJ}
#
#
${_SRC_OBJ}: ${SRC_OBJ} 

${SRC_OBJ}:%.o:%.c
	$(CC) $(CFLAGS) $(LIBS) -c $^ ${INC} -o ${OUTDIR}/$(notdir $@) 

${SUBDIRS}: ECHO
	make -C $@

ECHO:
	@echo $(SUBDIRS)

clean:
	rm -rf ${OUTDIR}

.PHONY: force
force:

install: force
	install -d ${prefix}/bin
	install -m 0755 ${OUTDIR}/${DEMO} ${prefix}/bin/
	install -d ${prefix}/lib/${DEMO}
	install -m 0755 ${OUTDIR}/lib/* ${prefix}/lib/${DEMO}/
	install -d ${prefix}/share/${DEMO}
	install -m 0644 conf/* ${prefix}/share/${DEMO}/
	install -d ${prefix}/include/${DEMO}
	install -m 0644 inc/* ${prefix}/include/${DEMO}/

uninstall:
	rm -rf ${prefix}/bin/${DEMO}
	rm -rf ${prefix}/share/${DEMO}
	rm -rf ${prefix}/lib/${DEMO}
	rm -rf ${prefix}/include/{DEMO}

